package org.xq.softcup.mq.registry.impl;

import org.xq.softcup.mq.XQRegistryClient;
import org.xq.softcup.mq.model.XQRegistryDataParamVO;
import org.xq.softcup.mq.registry.ServiceRegistry;

import java.util.*;

public class XQRegistryServiceRegistry extends ServiceRegistry {
    public static final String REGISTRY_ADDRESS = "REGISTRY_ADDRESS";
    public static final String ACCESS_TOKEN = "ACCESS_TOKEN";
    public static final String BIZ = "BIZ";
    public static final String ENV = "ENV";
    //注册客户端
    private XQRegistryClient xqRegistryClient;

    public XQRegistryClient getXQRegistryClient() {
        return this.xqRegistryClient;
    }
    //启动注册服务
    public void start(Map<String, String> param) {
        String registryAddress = (String) param.get(REGISTRY_ADDRESS);
        String accessToken = (String) param.get(ACCESS_TOKEN);
        String biz = (String) param.get(BIZ);
        String env = (String) param.get(ENV);

        biz = (biz != null) && (biz.trim().length() > 0) ? biz : "default";
        env = (env != null) && (env.trim().length() > 0) ? env : "default";

        this.xqRegistryClient = new XQRegistryClient(registryAddress, accessToken, biz, env);
    }

    public void stop() {
        if (this.xqRegistryClient != null) {
            this.xqRegistryClient.stop();
        }
    }

    public boolean registry(Set<String> keys, String value) {
        if ((keys == null) || (keys.size() == 0) || (value == null)) {
            return false;
        }
        List<XQRegistryDataParamVO> registryDataList = new ArrayList();
        for (String key : keys) {
            registryDataList.add(new XQRegistryDataParamVO(key, value));
        }
        return this.xqRegistryClient.registry(registryDataList);
    }

    public boolean remove(Set<String> keys, String value) {
        if ((keys == null) || (keys.size() == 0) || (value == null)) {
            return false;
        }
        List<XQRegistryDataParamVO> registryDataList = new ArrayList();
        for (String key : keys) {
            registryDataList.add(new XQRegistryDataParamVO(key, value));
        }
        return this.xqRegistryClient.remove(registryDataList);
    }

    public Map<String, TreeSet<String>> discovery(Set<String> keys) {
        return this.xqRegistryClient.discovery(keys);
    }

    public TreeSet<String> discovery(String key) {
        return this.xqRegistryClient.discovery(key);
    }
}
